package com.globallogic.bean;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.globallogic.consts.AppPages;
import com.globallogic.model.Md5Encoder;
import com.globallogic.model.service.UserService;
import com.globallogic.model.transferobject.UserTo;

@ManagedBean
@SessionScoped
public class UserBean {

	public UserBean() {
	  newUserStyle = activeStyle;	
	}
	
	public List<UserTo> getUsers() {
		return userService.getUsers();
	}

	public String newUser() {
		clearStyles();
		newUserStyle = activeStyle;
		return AppPages.registerNewUser + "?faces-redirect=true";
	}

	public String cancel() {
		return AppPages.adminDashboard + "?faces-redirect=true";
	}

	public String saveUser() {
		int result;

		result = userService.registerUser(user.getEntity());

		if (result == -1) {
			return "";
		} else {
			return AppPages.userList + "?faces-redirect=true";
		}

	}

	public String showUsers() {
		clearStyles();
		listUsersStyle = activeStyle;
		
		return AppPages.userList + "?faces-redirect=true";
	}

	public String getName() {
		return user.getName();
	}

	public void setName(String name) {
		user.setName(name);
	}

	public String getPassword() {
		return user.getPassword();
	}

	public void setPassword(String password) {
		user.setPassword(Md5Encoder.getEncodedValue(password));
	}

	public String getFullName() {
		return user.getFullName();
	}

	public void setFullName(String fullName) {
		user.setFullName(fullName);
	}

	public String getEmail() {
		return user.getEmail();
	}

	public void setEmail(String email) {
		user.setEmail(email);
	}

	public String getNotes() {
		return user.getNotes();
	}

	public void setNotes(String notes) {
		user.setNotes(notes);
	}

	public void setUserService(UserService value) {
		this.userService = value;
	}
	
	public String getNewUserStyle(){
		return newUserStyle;
	}
	
	public String getListUsersStyle(){
		return listUsersStyle;
	}
	
	public String getNewDailyPlanStyle(){
		return newDailyPlanStyle;		
	} 
	
	private String newUserStyle;
	private String listUsersStyle;
	private String newDailyPlanStyle;
	
	private void clearStyles(){
		newUserStyle = "";
		listUsersStyle = "";
		newDailyPlanStyle = "";		
	}

	private final String activeStyle = "active";
	@EJB
	private UserService userService;
	private UserTo user = new UserTo();

}
